﻿using System;
using System.Data;
using System.Diagnostics;

namespace Medianamik.Core.DAL
{
    [DebuggerDisplay("{PropertyId} - {CultureName} : {Value}")]
    public class NodePropertyValueLite : IEquatable<NodePropertyValueLite>
    {
        public Guid NodeId { get;  set; }
        public Guid PropertyId { get;  set; }
        public int? Revision { get;  set; }
        public string CultureName { get;  set; }
        public int? NodePropertyValueId { get;  set; }
        public string Value { get;  set; }

        public NodePropertyValueLite(IDataRecord reader)
        {
            NodeId = reader["NodeId"] as Guid? ?? Guid.Empty;
            NodePropertyValueId = (int)reader["NodePropertyValueId"];
            PropertyId = reader["PropertyId"] as Guid? ?? Guid.Empty;
            Revision = reader["Revision"] as int?;
            CultureName = reader["CultureName"] as string ?? string.Empty;
            Value = reader["Value"] as string;
        }

        public NodePropertyValueLite(NodeProperty nodeProperty)
        {
            //NodePropertyValueId - clé primaire de la table NodePropertyValue
            //on ne l'a plus avec la nouvelle approche
            //Du moins, ne sert plus a rien ?
 
            NodeId = nodeProperty.Node.ID;
            Value = nodeProperty.Value as string;
            CultureName = nodeProperty.Culture.Name;
            PropertyId = nodeProperty.NodeTypePropertyID;
        }

        public virtual bool Equals(NodePropertyValueLite other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other.Key.Equals(Key);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(NodePropertyValueLite)) return false;
            return Equals((NodePropertyValueLite)obj);
        }

        public override int GetHashCode()
        {
            return Key.GetHashCode();
        }

        private string  Key
        {
            get { return (PropertyId + CultureName); }
        }

        public NodePropertyValueLite() { }
    }
}
